<?php


namespace app\system\service;

class MemberAppService
{
	public static function get_user_list($options)
	{
		$where = self::get_user_conditions($options);
		$list = \think\Db::name("app_member_user")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_user")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$info = self::get_array_info($item);
			if ($info["code"] == \app\system\model\Code::success) {
				$data[] = $info["data"];
			} else {
				return ["code" => $info["code"], "msg" => $info["msg"]];
			}
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	private static function get_user_conditions($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["ids"]) && $options["ids"] != "") {
			$where[] = ["id", "in", $options["ids"]];
		}
		if (isset($options["account"]) && $options["account"] != "") {
			$where[] = ["account", "like", "%" . $options["account"] . "%"];
		}
		if (isset($options["nickname"]) && $options["nickname"] != "") {
			$where[] = ["nickname", "like", "%" . $options["nickname"] . "%"];
		}
		if (isset($options["disable"]) && $options["disable"] != "") {
			$where[] = ["disable", "=", $options["disable"]];
		}
		if (isset($options["is_forever_vip"]) && $options["is_forever_vip"] != "") {
			$where[] = ["is_forever_vip", "=", $options["is_forever_vip"]];
		}
		if (isset($options["level_id"]) && $options["level_id"] != "") {
			$where[] = ["level_id", "=", $options["level_id"]];
		}
		if (isset($options["superiors"]) && $options["superiors"] != "") {
			$where[] = ["superiors", "=", $options["superiors"]];
		}
		if (isset($options["phone"]) && $options["phone"] != "") {
			$where[] = ["phone", "=", $options["phone"]];
		}
		if (isset($options["mail"]) && $options["mail"] != "") {
			$where[] = ["mail", "=", $options["mail"]];
		}
		if (isset($options["vip_expire"]) && $options["vip_expire"] != "") {
			if ($options["vip_expire"] == 1) {
				$where[] = ["viptime", "<", time()];
			} else {
				$where[] = ["viptime", ">", time()];
			}
		}
		if (isset($options["online"]) && $options["online"] != "") {
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			$minimum_active_time = getUserMinimumActiveTime($app);
			if ($options["online"] == 1) {
				$where[] = ["acttime", ">=", $minimum_active_time];
				$where[] = ["force_offline_status", "=", 0];
			} else {
				$where[] = ["", "exp", \think\Db::raw("force_offline_status=1 or acttime<" . $minimum_active_time)];
			}
		}
		return $where;
	}
	public static function get_array_info($user)
	{
		$app = \think\Db::name("app")->where(["id" => $user["appid"]])->find();
		$CurrentLevel = \think\Db::name("app_member_user_level")->where([["appid", "=", $user["appid"]], ["id", "=", $user["level_id"]]])->find();
		if (empty($CurrentLevel)) {
			$default_level = \think\Db::name("app_member_user_level")->where([["appid", "=", $user["appid"]], ["is_default", "=", "1"]])->find();
			if (!empty($default_level)) {
				\think\Db::name("app_member_user")->where(["id" => $user["id"]])->update(["level_id" => $default_level["id"]]);
				$user["level_id"] = $default_level["id"];
				$CurrentLevel = $default_level;
			} else {
				return ["code" => \app\system\model\Code::error, "msg" => "用户关联的等级不存在或未关联用户等级,系统自动关联失败,查询到用户等级库内无默认等级"];
			}
		}
		$user = \app\api\common\member\Growth::check_upgrade($user);
		$NextLevel = \think\Db::name("app_member_user_level")->where([["appid", "=", $user["appid"]], ["upgrade", "=", "1"], ["level", ">", $CurrentLevel["level"]]])->find();
		$CappingGrowthValue = $NextLevel ? $NextLevel["integral"] : "full";
		$superiors = [];
		if ($user["superiors"]) {
			$superiors_data = \think\Db::name("app_member_user")->where(["id" => $user["superiors"]])->find();
			$superiors = ["uid" => $superiors_data["id"], "nickname" => $superiors_data["nickname"], "avatar" => $superiors_data["avatar"]];
		}
		$user["machine_code"] = getUserMachineCodeList($user["machine_code"]);
		$minimum_active_time = getUserMinimumActiveTime($app);
		$online_machine_list = \think\Db::name("app_member_user_login_machine")->where([["uid", "=", $user["id"]], ["active_time", ">", $minimum_active_time], ["force_offline_status", "=", 0], ["token_status", "=", 0]])->select();
		$user["online_machine_list"] = $online_machine_list;
		$user["is_on_line"] = count($online_machine_list) > 0 && $user["force_offline_status"] == 0;
		$user["private_data"] = get_app_user_private_data($app, $user["private_data"]);
		$user["user_current_level_info"] = ["id" => $CurrentLevel["id"], "level" => $CurrentLevel["level"], "name" => $CurrentLevel["name"]];
		$user["user_next_level_info"] = $NextLevel ? ["id" => $NextLevel["id"], "level" => $NextLevel["level"], "name" => $NextLevel["name"]] : [];
		$user["superiors_user_info"] = $superiors;
		$user["is_forever_vip"] = \boolval($user["is_forever_vip"]);
		$user["capping_growth_value"] = $CappingGrowthValue;
		$user["binding_ip_list"] = json_decode($user["binding_ip_list"], true) ?? [];
		return ["code" => \app\system\model\Code::success, "data" => $user];
	}
	public static function get_user_info($options)
	{
		$item = \think\Db::name("app_member_user")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$info = self::get_array_info($item);
		if ($info["code"] == \app\system\model\Code::success) {
			$data = $info["data"];
		} else {
			return ["code" => $info["code"], "msg" => $info["msg"]];
		}
		return ["code" => \app\system\model\Code::success, "data" => $data];
	}
	public static function add_user($options)
	{
		\think\Db::startTrans();
		try {
			$isSystemAuthUnpaid = isSystemAuthUnpaid();
			if ($isSystemAuthUnpaid && \think\Db::name("app_member_user")->where(["appid" => $options["appid"]])->count() > \app\common\model\FreeQuota::MemberAppUserQuota) {
				return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,一个应用最多只能注册" . \app\common\model\FreeQuota::MemberAppUserQuota . "个用户" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
			}
			$level = \think\Db::name("app_member_user_level")->where(["appid" => $options["appid"], "is_default" => 1])->find();
			if (!$level) {
				throw new \Exception("应用后台没有找到默认用户等级，请先在后台添加设置好用户默认等级");
			}
			$app = \think\Db::name("app")->where("id", $options["appid"])->find();
			$user = \think\Db::name("app_member_user")->where(["account" => $options["account"], "appid" => $options["appid"]])->find();
			if (!empty($user)) {
				throw new \Exception("该账号已存在");
			}
			$uid = \think\Db::name("app_member_user")->insertGetId(["appid" => $options["appid"], "account" => $options["account"], "level_id" => $level["id"], "password" => GeneratePassword($options["password"]), "nickname" => empty($options["nickname"]) ? "未设置昵称" : $options["nickname"], "machine_code" => empty($options["machine_code"]) ? "" : $options["machine_code"], "private_data" => json_encode($options["private_data"] ?? [], JSON_FORCE_OBJECT), "addtime" => time(), "phone" => $options["phone"] ?? "", "mail" => $options["mail"] ?? ""]);
			if (isset($options["is_forever_vip"]) && $options["is_forever_vip"]) {
				if (\intval($options["is_forever_vip"]) == 1) {
					\app\api\common\member\Finance::set($uid, "viptime", \app\common\model\DefaultConfig::PerpetualTimeValue, 0, "后台添加用户赠送", \app\common\model\MemberAppModel::UserVipTimeLogSourceAdminControl);
				} else {
					\app\api\common\member\Finance::set($uid, "viptime", $options["viptime"], 0, "后台添加用户赠送", \app\common\model\MemberAppModel::UserVipTimeLogSourceAdminControl);
				}
			} else {
				if (!empty($options["viptime"])) {
					\app\api\common\member\Finance::set($uid, "viptime", $options["viptime"], 0, "后台添加用户赠送", \app\common\model\MemberAppModel::UserVipTimeLogSourceAdminControl);
				}
			}
			if (!empty($options["point"])) {
				\app\api\common\member\Finance::set($uid, "point", $options["point"], 0, "后台添加用户赠送", \app\common\model\MemberAppModel::UserPointLogSourceAdminControl);
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "添加成功"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit_user($options)
	{
		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where("id", $options["appid"])->find();
			$user = \think\Db::name("app_member_user")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
			if (empty($user)) {
				throw new \Exception("id不存在");
			}
			$level = \think\Db::name("app_member_user_level")->where(["appid" => $options["appid"], "id" => $options["level_id"]])->find();
			if (empty($level)) {
				throw new \Exception("等级不存在");
			}
			$data = ["machine_code_binding_top_limit" => $options["machine_code_binding_top_limit"] ?? 0, "online_machine_upper_limit" => $options["online_machine_upper_limit"] ?? 0, "ip_binding_top_limit" => $options["ip_binding_top_limit"] ?? 0, "machine_ip_binding_top_limit" => $options["machine_ip_binding_top_limit"] ?? 0, "nickname" => empty($options["nickname"]) ? "未设置昵称" : $options["nickname"], "machine_code" => empty($options["machine_code"]) ? "" : json_encode($options["machine_code"], JSON_UNESCAPED_UNICODE), "binding_ip_list" => empty($options["binding_ip_list"]) ? "" : json_encode($options["binding_ip_list"], JSON_UNESCAPED_UNICODE), "level_id" => $options["level_id"], "private_data" => json_encode($options["private_data"] ?? [], JSON_FORCE_OBJECT), "phone" => empty($options["phone"]) ? $user["phone"] : $options["phone"], "mail" => empty($options["mail"]) ? $user["mail"] : $options["mail"]];
			if (!empty($options["unbinding_machine_code"]) || empty($options["machine_code"]) && $user["machine_code"]) {
				$data["machine_code"] = "";
				$data["force_offline_status"] = 1;
				\think\Db::name("app_member_user_unbinding_log")->insert(["appid" => $options["appid"], "user_id" => $user["id"], "time" => time(), "source" => \app\common\model\MemberAppModel::UserUnBindingLogSourceAdminControl]);
			}
			if (isset($options["is_forever_vip"]) && $options["is_forever_vip"] != $user["is_forever_vip"]) {
				$data["is_forever_vip"] = \intval($options["is_forever_vip"]);
				if (\intval($options["is_forever_vip"]) == 1) {
					\app\api\common\member\Finance::set($user, "viptime", \app\common\model\DefaultConfig::PerpetualTimeValue, 0, "后台操作", \app\common\model\MemberAppModel::UserVipTimeLogSourceAdminControl);
				} else {
					\app\api\common\member\Finance::set($user, "viptime", $options["viptime"], 0, "后台操作", \app\common\model\MemberAppModel::UserVipTimeLogSourceAdminControl);
				}
			} else {
				if (!empty($options["viptime"]) && $options["viptime"] != $user["viptime"]) {
					\app\api\common\member\Finance::set($user, "viptime", $options["viptime"], 0, "后台操作", \app\common\model\MemberAppModel::UserVipTimeLogSourceAdminControl);
				}
			}
			if (isset($options["point"]) && $options["point"] != $user["point"]) {
				$point_type = $options["point"] > $user["point"] ? 0 : 1;
				if ($point_type == 0) {
					$point_number = $options["point"] - $user["point"];
				} else {
					$point_number = $user["point"] - $options["point"];
				}
				\app\api\common\member\Finance::set($user, "point", $point_number, $point_type, "后台操作", \app\common\model\MemberAppModel::UserPointLogSourceAdminControl);
			}
			if (!empty($options["new_password"])) {
				memberUserPasswordChange($user, $options["new_password"]);
			}
			\think\Db::name("app_member_user")->where(["id" => $user["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "编辑成功"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_user($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_member_user")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_user")->where(["id" => $options["id"], "appid" => $options["appid"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function user_disable($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_user")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_user")->where(["id" => $options["id"]])->update(["disable" => !$row["disable"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_card_list($options)
	{
		$where = self::get_card_conditions($options);
		$list = \think\Db::name("app_member_card")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_card")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$item["batch_info"] = \think\Db::name("app_member_card_batch")->where(["id" => $item["batch_id"]])->find();
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	private static function get_card_conditions($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["ids"]) && $options["ids"] != "") {
			$where[] = ["id", "in", $options["ids"]];
		}
		if (isset($options["card_list"]) && $options["card_list"] != "") {
			// 使用 BINARY 关键字实现区分大小写的 IN 查询
			$cardList = is_array($options["card_list"]) ? $options["card_list"] : explode(',', $options["card_list"]);
			$cardListEscaped = array_map(function($card) {
				return "'" . addslashes($card) . "'";
			}, $cardList);
			$where[] = ["", "exp", \think\Db::raw("BINARY `card` IN (" . implode(',', $cardListEscaped) . ")")];
		}
		if (isset($options["batch_number"]) && $options["batch_number"] != "") {
			$batch = \think\Db::name("app_member_card_batch")->where([["number", "like", "%{$options["batch_number"]}%"]])->find();
			if (!empty($batch)) {
				$where[] = ["batch_id", "=", $batch["id"]];
			}
		}
		if (isset($options["batch_remarks"]) && $options["batch_remarks"] != "") {
			$batch = \think\Db::name("app_member_card_batch")->where([["remarks", "like", "%{$options["batch_remarks"]}%"]])->find();
			if (!empty($batch)) {
				$where[] = ["batch_id", "=", $batch["id"]];
			}
		}
		if (isset($options["useuid"]) && $options["useuid"] != "") {
			$where[] = ["useuid", "=", $options["useuid"]];
		}
		if (isset($options["batch_id"]) && $options["batch_id"] != "") {
			$where[] = ["batch_id", "=", $options["batch_id"]];
		}
		if (isset($options["type"]) && $options["type"] != "") {
			$where[] = ["type", "=", $options["type"]];
			if ($options["type"] == 0) {
				if (isset($options["time_type"])) {
					$where[] = ["time_type", "=", $options["time_type"]];
				}
			}
		}
		if (isset($options["value"]) && $options["value"] != "") {
			$where[] = ["value", "=", $options["value"]];
		}
		if (isset($options["status"]) && $options["status"] != "") {
			$where[] = ["status", "=", $options["status"]];
		}
		if (isset($options["time_section_type"]) && $options["time_section_type"] != "" && !empty($options["time_section_start"])) {
			$time_section_field = "addtime";
			if ($options["time_section_type"] == 1) {
				$time_section_field = "usetime";
			}
			$where[] = [$time_section_field, ">=", strtotime($options["time_section_start"])];
			if (!empty($options["time_section_end"])) {
				$where[] = [$time_section_field, "<", strtotime($options["time_section_end"])];
			}
		}
		if (isset($options["agent_id"]) && $options["agent_id"] != "") {
			$where[] = ["agent_id", "=", $options["agent_id"]];
		}
		if (isset($options["card"]) && $options["card"] != "") {
			// 使用 BINARY 关键字实现区分大小写的模糊查询
			$where[] = ["", "exp", \think\Db::raw("BINARY `card` LIKE '%" . addslashes($options["card"]) . "%'")];
		}
		if (isset($options["remarks"]) && $options["remarks"] != "") {
			$where[] = ["remarks", "like", "%" . $options["remarks"] . "%"];
		}
		return $where;
	}
	public static function get_card_batch_list($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["agent_id"]) && $options["agent_id"] != "") {
			$where[] = ["agent_id", "=", $options["agent_id"]];
		}
		if (isset($options["number"]) && $options["number"] != "") {
			$where[] = ["number", "like", "%" . $options["number"] . "%"];
		}
		if (isset($options["remarks"]) && $options["remarks"] != "") {
			$where[] = ["remarks", "like", "%" . $options["remarks"] . "%"];
		}
		$list = \think\Db::name("app_member_card_batch")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_card_batch")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$item["card_info"] = ["total" => \think\Db::name("app_member_card")->where(["batch_id" => $item["id"]])->count(), "used_total" => \think\Db::name("app_member_card")->where(["status" => 1, "batch_id" => $item["id"]])->count(), "not_used_total" => \think\Db::name("app_member_card")->where(["status" => 0, "batch_id" => $item["id"]])->count()];
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages, "all_card_data" => ["total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"]])->count(), "used_total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"], "status" => 1])->count(), "not_used_total" => \think\Db::name("app_member_card")->where(["status" => 0, "appid" => $options["appid"]])->count(), "hour_total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"], "time_type" => 1])->count(), "day_total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"], "time_type" => 2])->count(), "week_total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"], "time_type" => 3])->count(), "month_total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"], "time_type" => 4])->count(), "year_total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"], "time_type" => 5])->count(), "perpetual_total" => \think\Db::name("app_member_card")->where(["appid" => $options["appid"], "time_type" => 6])->count()]];
	}
	public static function add_card($options)
	{
		$num = $options["num"];
		if (\app\common\server\ConfigServer::get("system", \app\common\model\DefaultConfig::SystemTestDemoField, false) && $num > 2) {
			return ["code" => \app\system\model\Code::fail, "msg" => "当前站点为演示站点,一次最多只能生成2个" . \app\common\model\DefaultConfig::SystemTestDemoTipsSuffixText];
		}
		$isSystemAuthUnpaid = isSystemAuthUnpaid();
		if ($isSystemAuthUnpaid && \app\common\model\FreeQuota::MemberAddCardLimitNum < $num) {
			return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,一次最多只能生成" . \app\common\model\FreeQuota::MemberAddCardLimitNum . ",您可分多次生成" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
		}
		if ($isSystemAuthUnpaid && \think\Db::name("app_member_card")->where(["appid" => $options["appid"]])->count() + $num > \app\common\model\FreeQuota::MemberAppCardQuota) {
			return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,一个应用最多只能存储" . \app\common\model\FreeQuota::MemberAppCardQuota . "个卡密" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
		}
		$today = \think\helper\Time::today();
		if ($isSystemAuthUnpaid && \think\Db::name("app_member_card")->where([["appid", "=", $options["appid"]], ["addtime", "between", $today]])->count() + $num > \app\common\model\FreeQuota::MemberAppDailyAddCard) {
			return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,一个应用每天最多只能生成" . \app\common\model\FreeQuota::MemberAppDailyAddCard . "个卡密" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
		}
		if ($isSystemAuthUnpaid && \think\Db::name("app")->count() > \app\common\model\FreeQuota::AppQuota) {
			return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,最多只能拥有" . \app\common\model\FreeQuota::AppQuota . "个应用" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
		}

		// 临时提高内存限制，防止导入大量卡密时内存不足
		@ini_set('memory_limit', '512M');
		// 设置最大执行时间为 5 分钟
		@set_time_limit(300);

		\think\Db::startTrans();
		try {
			$app = \think\Db::name("app")->where("id", $options["appid"])->find();
			$list = [];
			$head = empty($options["head"]) ? "" : $options["head"];
			$foot = empty($options["foot"]) ? "" : $options["foot"];
			$batch_number = uniqid();
			$batch_id = \think\Db::name("app_member_card_batch")->insertGetId(["appid" => $app["id"], "time" => time(), "num" => $options["num"], "number" => $batch_number, "remarks" => $options["batch_remarks"] ?? "", "card_type" => $options["type"] ?? 0, "card_time_type" => $options["time_type"] ?? 0, "card_value" => $options["value"] ?? 1]);
			for ($i = 0; $i < $options["num"]; $i++) {
				$body = getAddCardBodyStructure($options["body"] ?? 0, $options["transform"] ?? 0, $options["card_length"] ?? 0);
				$card = $head . $body . $foot;
				$list[] = $card;
				\think\Db::name("app_member_card")->insert(["appid" => $options["appid"], "type" => $options["type"], "time_type" => $options["time_type"] ?? 0, "value" => $options["value"] ?? 1, "addtime" => time(), "card" => $card, "batch_id" => $batch_id]);
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "data" => ["list" => $list, "string" => implode("\n", $list), "batch" => ["id" => $batch_id, "number" => $batch_number]], "msg" => "生成成功"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_card($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_member_card")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_card")->where(["id" => $options["id"], "appid" => $options["appid"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_card_batch($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_member_card_batch")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_card_batch")->where(["id" => $options["id"], "appid" => $options["appid"]])->delete();
			\think\Db::name("app_member_card")->where(["batch_id" => $options["id"], "appid" => $options["appid"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function export_member_card_table($options)
	{
		// 临时提高内存限制，防止导出大量数据时内存不足
		@ini_set('memory_limit', '512M');
		// 设置最大执行时间为 5 分钟
		@set_time_limit(300);

		$result = MemberAppService::get_card_list($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$list = $result["data"];
			if (empty($list)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "=>无数据导出"];
			}
			$data[] = ["ID", "卡密", "类型", "时长类型", "面值", "状态", "生成时间", "使用时间", "使用用户ID", "批次ID", "代理ID"];
			foreach ($list as $item) {
				// 确保所有字段都是字符串类型，避免 PHPExcel 类型错误
				$row = [
					(string)$item["id"],
					(string)$item["card"],
					(string)\app\common\model\CardType::getName($item["type"]),
					$item["type"] == \app\common\model\CardType::Time[0] ? (string)\app\common\model\CardTimeType::getName($item["time_type"]) : "",
					(string)$item["value"],
					$item["status"] == 1 ? "已使用" : "未使用",
					date("Y-m-d H:i:s", $item["addtime"]),
					$item["usetime"] ? date("Y-m-d H:i:s", $item["usetime"]) : "",
					(string)($item["useuid"] ?? ""),
					(string)($item["batch_id"] ?? ""),
					(string)($item["agent_id"] ?? "")
				];
				$data[] = $row;
			}
			unset($list);
			$res = \app\common\server\Export::export($options["export_file_type"], "充值卡卡密导出_" . date("Y-m-d H:i:s"), $data, 1);
			return ["code" => \app\system\model\Code::success, "msg" => "文件生成完成", "data" => ExportService::export_return_data_structure($res)];
		} else {
			return $result;
		}
	}
	public static function get_user_level_list($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%" . $options["name"] . "%"];
		}
		$list = \think\Db::name("app_member_user_level")->where($where)->order(["level" => "asc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_user_level")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_level_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	private static function get_level_array_info($item)
	{
		$item["statistics"] = ["user_count" => \think\Db::name("app_member_user")->where(["appid" => $item["appid"], "level_id" => $item["id"]])->count()];
		return $item;
	}
	public static function get_user_level_info($options)
	{
		$item = \think\Db::name("app_member_user_level")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_level_array_info($item)];
	}
	public static function add_user_level($options)
	{
		\think\Db::startTrans();
		try {
			$name = \think\Db::name("app_member_user_level")->where(["name" => $options["name"], "appid" => $options["appid"]])->find();
			if (!empty($name)) {
				throw new \Exception("该等级名称已存在");
			}
			$level = \think\Db::name("app_member_user_level")->where(["level" => $options["level"], "appid" => $options["appid"]])->find();
			if (!empty($level)) {
				throw new \Exception("该等级号已存在");
			}
			$original_default_level = \think\Db::name("app_member_user_level")->where(["appid" => $options["appid"], "is_default" => 1])->find();
			$level_id = \think\Db::name("app_member_user_level")->insertGetId(["appid" => $options["appid"], "name" => $options["name"], "level" => $options["level"], "integral" => $options["integral"], "remarks" => $options["remarks"] ?? "", "upgrade" => $options["upgrade"], "is_default" => $options["is_default"], "addtime" => time()]);
			if ($options["is_default"] === 1 && $original_default_level) {
				if (!$options["upgrade"]) {
					throw new \Exception("默认等级必须设置为用户可升级");
				}
				if ($options["integral"] <= $original_default_level["integral"]) {
					throw new \Exception("您设置当前为默认等级，但所需成长值必须大于原来的默认等级所需成长值，否则用户将会直接升级");
				}
				\think\Db::name("app_member_user_level")->where("id", $original_default_level["id"])->update(["is_default" => 0]);
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "添加成功"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit_user_level($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_user_level")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
			if (empty($row)) {
				throw new \Exception("该等级ID不存在");
			}
			$name = \think\Db::name("app_member_user_level")->where(["name" => $options["name"], "appid" => $options["appid"]])->find();
			if (!empty($name) && $row["id"] != $name["id"]) {
				throw new \Exception("该等级名称已存在");
			}
			$level = \think\Db::name("app_member_user_level")->where(["level" => $options["level"], "appid" => $options["appid"]])->find();
			if (!empty($level) && $row["id"] != $level["id"]) {
				throw new \Exception("该等级号已存在");
			}
			$original_default_level = \think\Db::name("app_member_user_level")->where(["appid" => $options["appid"], "is_default" => 1])->find();
			$data = ["name" => $options["name"], "level" => $options["level"], "integral" => $options["integral"], "remarks" => $options["remarks"] ?? "", "upgrade" => $options["upgrade"], "is_default" => $options["is_default"]];
			\think\Db::name("app_member_user_level")->where(["id" => $row["id"]])->update($data);
			$level_id = $row["id"];
			if ($options["is_default"] === 1 && $original_default_level && $original_default_level["id"] != $row["id"]) {
				if (!$options["upgrade"]) {
					throw new \Exception("默认等级必须设置为用户可升级");
				}
				if ($options["integral"] <= $original_default_level["integral"]) {
					throw new \Exception("您设置当前为默认等级，但所需成长值必须大于原来的默认等级所需成长值，否则用户将会直接升级");
				}
				\think\Db::name("app_member_user_level")->where("id", $original_default_level["id"])->update(["is_default" => 0]);
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "编辑成功"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_user_level($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_user_level")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				throw new \Exception("id不存在");
			}
			if ($row["is_default"] == 1) {
				throw new \Exception("不支持删除默认等级");
			}
			if (\think\Db::name("app_member_user")->where(["appid" => $options["appid"], "level_id" => $row["id"]])->count()) {
				throw new \Exception("该等级下有关联的用户,安全起见暂不支持删除");
			}
			\think\Db::name("app_member_user_level")->where(["id" => $options["id"], "appid" => $options["appid"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function user_level_move_new_level($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_user_level")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "被转移等级id不存在"];
			}
			$new = \think\Db::name("app_member_user_level")->where(["id" => $options["new_level_id"], "appid" => $options["appid"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "目标等级id不存在"];
			}
			if (\think\Db::name("app_member_user")->where(["appid" => $options["appid"], "level_id" => $row["id"]])->count() == 0) {
				throw new \Exception("被转移等级没有相关联的用户无需转移");
			}
			\think\Db::name("app_member_user")->where(["level_id" => $row["id"], "appid" => $options["appid"]])->update(["level_id" => $new["id"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_user_account_finance_log($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["uid"]) && $options["uid"] != "") {
			$where[] = ["uid", "=", $options["uid"]];
		}
		if (isset($options["field"]) && $options["field"] != "") {
			$where[] = ["field", "=", $options["field"]];
		}
		if (isset($options["type"]) && $options["type"] != "") {
			$where[] = ["type", "=", $options["type"]];
		}
		if (isset($options["source"]) && $options["source"] != "") {
			$where[] = ["source", "=", $options["source"]];
		}
		if (isset($options["remarks"]) && $options["remarks"] != "") {
			$where[] = ["remarks", "like", "%" . $options["remarks"] . "%"];
		}
		if (!empty($options["time_scope"]) && count($options["time_scope"]) > 1) {
			$time_scope = $options["time_scope"];
			$where[] = ["time", ">=", strtotime($time_scope[0])];
			$where[] = ["time", "<", strtotime($time_scope[1])];
		}
		$list = \think\Db::name("app_member_user_finance_log")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_user_finance_log")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$item["user_info"] = \think\Db::name("app_member_user")->where(["id" => $item["uid"]])->field("id,nickname,avatar,account")->find();
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_user_unbinding_log($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["uid"]) && $options["uid"] != "") {
			$where[] = ["user_id", "=", $options["uid"]];
		}
		if (isset($options["source"]) && $options["source"] != "") {
			$where[] = ["source", "=", $options["source"]];
		}
		$list = \think\Db::name("app_member_user_unbinding_log")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_user_unbinding_log")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$item["user_info"] = \think\Db::name("app_member_user")->where(["id" => $item["user_id"]])->field("id,nickname,avatar,account")->find();
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function condition_force_offline_user($options)
	{
		\think\Db::startTrans();
		try {
			$where = self::get_user_conditions($options);
			$count = \think\Db::name("app_member_user")->where($where)->count();
			\think\Db::name("app_member_user")->where($where)->update(["force_offline_status" => 1]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "成功下线{$count}个在线卡密"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function user_force_offline($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_member_user")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_user")->where(["id" => $options["id"]])->update(["force_offline_status" => 1]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function user_force_offline_machine($options)
	{
		\think\Db::startTrans();
		try {
			$login_machine = \think\Db::name("app_member_user_login_machine")->where(["id" => $options["id"]])->find();
			if (empty($login_machine)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$row = \think\Db::name("app_member_user")->where(["id" => $login_machine["uid"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "卡密不存在"];
			}
			\think\Db::name("app_member_user_login_machine")->where(["id" => $login_machine["id"]])->update(["force_offline_status" => 1]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function condition_del_user($options)
	{
		\think\Db::startTrans();
		try {
			$where = self::get_user_conditions($options);
			$count = \think\Db::name("app_member_user")->where($where)->count();
			\think\Db::name("app_member_user")->where($where)->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "成功删除{$count}个用户"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function condition_unbinding_user($options)
	{
		\think\Db::startTrans();
		try {
			$where = self::get_user_conditions($options);
			$count = \think\Db::name("app_member_user")->where($where)->count();
			\think\Db::name("app_member_user")->where($where)->update(["machine_code" => ""]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "成功解绑{$count}个用户"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function condition_increase_duration_user($options)
	{
		\think\Db::startTrans();
		try {
			$where = self::get_user_conditions($options);
			$list = \think\Db::name("app_member_user")->where($where)->select();
			$s = 0;
			$time = time();
			foreach ($list as $item) {
				$suffix_tips = "";
				$s++;
				if ($options["value"] === "永久") {
					$number = \app\common\model\DefaultConfig::PerpetualTimeValue;
				} else {
					if ($time < $item["viptime"]) {
						$number = $item["viptime"] + $options["value"];
					} else {
						$number = $time + $options["value"];
						$suffix_tips = "(因会员过期则变更为" . ($number - $item["viptime"]) . "秒)";
					}
				}
				\app\api\common\member\Finance::set($item, "viptime", $number, 0, "条件筛选增加时长{$options["value"]}秒" . $suffix_tips, \app\common\model\MemberAppModel::UserVipTimeLogSourceUserConditionIncreaseDuration);
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "成功增加会员时长" . $s . "个用户"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function condition_increase_point_user($options)
	{
		\think\Db::startTrans();
		try {
			$where = self::get_user_conditions($options);
			$list = \think\Db::name("app_member_user")->where($where)->select();
			foreach ($list as $item) {
				if ($options["value"] > 0) {
					$number = $options["value"];
					$type = 0;
				} else {
					$number = 0 - $options["value"];
					$type = 1;
				}
				\app\api\common\member\Finance::set($item, "point", $number, $type, "条件筛选操作增减 {$options["value"]}", \app\common\model\MemberAppModel::UserPointLogSourceUserConditionIncreaseDuration);
			}
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "成功增加" . count($list) . "个用户"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function condition_disable_user($options)
	{
		\think\Db::startTrans();
		try {
			$where = self::get_user_conditions($options);
			$count = \think\Db::name("app_member_user")->where($where)->count();
			\think\Db::name("app_member_user")->where($where)->update(["disable" => $options["value"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "成功设置{$count}个用户禁启用状态"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_settlement_list($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (!empty($options["time_scope"]) && count($options["time_scope"]) > 1) {
			$time_scope = $options["time_scope"];
			$where[] = ["apply_time", ">=", strtotime($time_scope[0])];
			$where[] = ["apply_time", "<=", strtotime($time_scope[1])];
		}
		if (isset($options["status"]) && $options["status"] != "") {
			$where[] = ["status", "=", $options["status"]];
		}
		if (isset($options["uid"]) && $options["uid"] != "") {
			$where[] = ["uid", "=", $options["uid"]];
		}
		$list = \think\Db::name("app_member_user_settlement")->where($where)->order("id", "desc")->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_user_settlement")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$info = self::get_array_info(\think\Db::name("app_member_user")->where(["id" => $item["uid"]])->find());
			if ($info["code"] == \app\system\model\Code::success) {
				$item["user_info"] = $info["data"];
			} else {
				return ["code" => $info["code"], "msg" => $info["msg"]];
			}
			$item["settlement_info"] = json_decode($item["settlement_info"], true);
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_settlement_info($options)
	{
		$item = \think\Db::name("app_member_user_settlement")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "该结算申请不存在"];
		}
		$info = self::get_array_info(\think\Db::name("app_member_user")->where(["id" => $item["uid"]])->find());
		if ($info["code"] == \app\system\model\Code::success) {
			$item["user_info"] = $info["data"];
		} else {
			return ["code" => $info["code"], "msg" => $info["msg"]];
		}
		$item["settlement_info"] = json_decode($item["settlement_info"], true);
		return ["code" => \app\system\model\Code::success, "data" => $item];
	}
	public static function processing_settlement($options)
	{
		\think\Db::startTrans();
		try {
			$settlement = \think\Db::name("app_member_user_settlement")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
			if (empty($settlement)) {
				\think\Db::rollback();
				return ["code" => \app\system\model\Code::fail, "msg" => "该结算申请不存在"];
			}
			if ($settlement["status"] != 0) {
				\think\Db::rollback();
				return ["code" => \app\system\model\Code::fail, "msg" => "该结算已经处理"];
			}
			$settlement_info = json_decode($settlement["settlement_info"], true);
			if ($options["status"] == 2) {
				if (empty($options["reason"])) {
					\think\Db::rollback();
					return ["code" => \app\system\model\Code::fail, "msg" => "请输入驳回原因"];
				}
				\think\Db::name("store_settlement")->where(["id" => $settlement["id"]])->update(["status" => 2, "solve_time" => time(), "admin_id" => $options["admin_id"], "admin_name" => $options["admin_name"], "reject_reason" => $options["reason"]]);
				$point_finance_log = \think\Db::name("app_member_user_finance_log")->where(["extra_data" => $settlement["id"], "uid" => $settlement["uid"]])->find();
				if ($point_finance_log) {
					$user = \think\Db::name("app_member_user")->where(["id" => $settlement["uid"]])->find();
					\app\api\common\member\Finance::set($user, "point", $point_finance_log["number"], 0, "积分现金结算被驳回退回积分", \app\common\model\MemberAppModel::UserPointLogSourceRejectPointCashSettlementIncrease, $settlement["id"]);
				}
				\think\Db::commit();
				return ["code" => \app\system\model\Code::success, "msg" => "驳回成功"];
			}
			\think\Db::name("app_member_user_settlement")->where(["id" => $settlement["id"]])->update(["status" => 1, "solve_time" => time(), "admin_id" => $options["admin_id"], "admin_name" => $options["admin_name"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "结算成功"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::fail, "msg" => "操作失败 " . $e->getMessage()];
		}
	}
	public static function get_login_machine_list($options)
	{
		$item = \think\Db::name("app_member_user")->where(["id" => $options["id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		$where[] = ["uid", "=", $options["id"]];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["machine_code"]) && $options["machine_code"] != "") {
			$where[] = ["machine_code", "like", "%" . $options["machine_code"] . "%"];
		}
		if (isset($options["ip"]) && $options["ip"] != "") {
			$where[] = ["ip", "like", "%" . $options["ip"] . "%"];
		}
		$list = \think\Db::name("app_member_user_login_machine")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_member_user_login_machine")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		$app = \think\Db::name("app")->where(["id" => $item["appid"]])->find();
		$minimum_active_time = getUserMinimumActiveTime($app);
		foreach ($list as $item) {
			$item["is_on_line"] = $minimum_active_time < $item["active_time"];
			$item["binding_ip_list"] = json_decode($item["binding_ip_list"], true) ?? [];
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function del_login_machine($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_member_user_login_machine")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_member_user_login_machine")->where(["id" => $options["id"], "appid" => $options["appid"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}